﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YwhCore
{
    public class RoomService
    {
        public static QueryRsp<Room> Save(Room room)
        {
            QueryRsp<Room> rsp = new QueryRsp<Room>();

            ConfigService.Fsql.InsertOrUpdate<Room>().SetSource(room).ExecuteAffrows();

            return rsp;
        }

        public static QueryRsp<Room> List(int PageNumber, int PageSize, int phaseNo = 0, int buildingNo = 0, int unitNo = 0)
        {
            long l = 0;
            QueryRsp<Room> query = new QueryRsp<Room>();
            FreeSql.ISelect<Room> select = ConfigService.Fsql.Select<Room>();
            if (phaseNo > 0)
            {
                select = select.Where(x => x.PhaseNo == phaseNo);
                if (buildingNo > 0)
                {
                    select.Where(x => x.BuildingNo == buildingNo);
                    if (unitNo > 0)
                    {
                        select = select.Where(x => x.UnitNo == unitNo);
                    }
                }
            }

            query.Items=select.Count(out l).Page(PageNumber, PageSize).ToList();

            query.Total = (int)l;
            return query;
        }

        public static QueryRsp<Room> Update(Room room)
        {
            QueryRsp<Room> rsp = new QueryRsp<Room>();

            ConfigService.Fsql.Update<Room>().Set(a => a.PhaseNo, 1).ExecuteAffrows();

            return rsp;
        }

        public static QueryRsp<Room> Delete(Room room)
        {
            QueryRsp<Room> rsp = new QueryRsp<Room>();

            ConfigService.Fsql.Delete<Room>().Where(x => x.RoomNo == room.RoomNo).ExecuteAffrows();

            return rsp;
        }
    }
}
